package com.ss.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ss.bean.*;
import com.ss.mapper.*;
import com.ss.result.R;
import com.ss.service.EstateService;
import com.ss.vo.CellMessage;
import com.ss.vo.UnitMessage;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: ljy.s
 * @Date: 2022/9/17 - 09 - 17 - 1:10
 */
/*实现类*/
@Service
public class EstateServiceImpl implements EstateService {

    @Resource
    private TblCompanyMapper companyMapper;

    @Resource
    private FcEstateMapper fcEstateMapper;

    @Resource
    private FcBuildingMapper fcBuildingMapper;

    @Resource
    private FcUnitMapper fcUnitMapper;

    @Resource
    private FcCellMapper fcCellMapper;

    @Override
    public List<TblCompany> selectCompany() {
        List<TblCompany> tblCompanies = companyMapper.selectCompany();
        return tblCompanies;
    }

    /**
     * 接收前端输入的住宅基本信息,保存
     * 在做真正的数据插入之前，应该对住宅编码做一个判断，判断当前住宅编码是否唯一
     *
     * @param fcEstate
     * @return
     */
    @Override
    public Integer insertEstate(FcEstate fcEstate) {
        Integer result = 0;
        QueryWrapper<FcEstate> queryWrapper = new QueryWrapper<>();
        // 判断住宅编码是否唯一
        queryWrapper.eq("estate_code", fcEstate.getEstateCode());
        FcEstate fe = fcEstateMapper.selectOne(queryWrapper);
        // 如果数据不存在，则正常插入
        if (fe == null) {
            result = fcEstateMapper.insert(fcEstate);
        }
        return result;
    }

    /**
     * 根据传递的楼宇数量和住宅编码来生成对应的楼宇信息，同时回显
     *
     * @param buildingNumber 楼宇数量
     * @param estateCode     房产(住宅)编码
     * @return 回显的数据
     */
    @Override
    public List<FcBuilding> selectBuilding(Integer buildingNumber, String estateCode) {
        List<FcBuilding> fcBuildings = new ArrayList<>();
        for (Integer i = 0; i < buildingNumber; i++) {
            FcBuilding fcBuilding = new FcBuilding();
            // 添加唯一标识
            fcBuilding.setBuildingCode(estateCode + "B" + (i + 1));
            fcBuilding.setBuildingName("第" + (i + 1) + "号楼");
            fcBuilding.setEstateCode(estateCode);
            fcBuildingMapper.insert(fcBuilding);
            fcBuildings.add(fcBuilding);
        }
        return fcBuildings;
    }

    /**
     * 完成楼宇的数据更新
     *
     * @param fcBuilding 用户编写的楼宇数据
     * @return 更新是否成功
     */
    @Override
    public Integer updateBuilding(FcBuilding fcBuilding) {
        Integer result = fcBuildingMapper.updateById(fcBuilding);
        return result;
    }

    /**
     * 维护单元信息
     *
     * @return 回显单元信息
     */
    @Override
    public List<FcUnit> selectUnit(UnitMessage unitMessage) {
        // 定义返回的集合
        List<FcUnit> fcUnits = new ArrayList<>();
        // 插入数据的操作
        for (Integer i = 0; i < unitMessage.getUnitCount(); i++) {
            FcUnit fcUnit = new FcUnit();
            fcUnit.setBuildingCode(unitMessage.getBuildingCode());
            // 添加唯一标识
            fcUnit.setUnitCode(unitMessage.getBuildingCode() + "U" + (i + 1));
            fcUnit.setUnitName("第" + (i + 1) + "单元");
            fcUnitMapper.insert(fcUnit);
            fcUnits.add(fcUnit);
        }
        return fcUnits;
    }

    /**
     * 完成单元信息的更新
     *
     * @param fcUnit 用户编写的单元信息
     * @return 更新是否成功
     */
    @Override
    public Integer updateUnit(FcUnit fcUnit) {
        Integer result = fcUnitMapper.updateById(fcUnit);
        return result;
    }

    /**
     * 添加房间信息
     *
     * @param cellMessage 房间信息
     * @return 是否添加成功
     */
    @Override
    public List<FcCell> insertCell(CellMessage cellMessage) {
        List<FcCell> fcCells = new ArrayList<>();
        // 楼层
        for (Integer i = 0; i < cellMessage.getStopFloor(); i++) {
            // 房间
            for (Integer j = 0; j < cellMessage.getStopCellId(); j++) {
                FcCell fcCell = new FcCell();
                fcCell.setUnitCode(cellMessage.getUnitCode());// 单元编码
                // 添加唯一标识
                fcCell.setCellCode(cellMessage.getUnitCode() + "C" + (i + 1) + "0" + j);// 房间编码 C203
                fcCell.setCellName((i + 1) + "0" + j);// 房间名称 203
                fcCell.setFloorNumber(cellMessage.getStopFloor());// 楼层数 获取结束的楼层数
                fcCellMapper.insert(fcCell);// 插入数据
                fcCells.add(fcCell);
            }
        }
        return fcCells;
    }

    /**
     * 根据楼宇编码查询对应信息
     *
     * @param estateCode 楼宇编码
     * @return 返回对应楼宇信息
     */
    @Override
    public List<FcBuilding> selectBuildingByEstate(String estateCode) {
        QueryWrapper<FcBuilding> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("estate_code", estateCode);
        // 通过此方法可以规定查询的字段
        queryWrapper.select("building_code", "building_name");
        List<FcBuilding> fcBuildings = fcBuildingMapper.selectList(queryWrapper);
        return fcBuildings;
    }

    /**
     * 根据单元编码查询对应信息
     *
     * @param buildingCode 单元编码
     * @return 返回对应单元信息
     */
    @Override
    public List<FcUnit> selectUnitByBuildingCode(String buildingCode) {
        QueryWrapper<FcUnit> queryWrapper = new QueryWrapper<>();
        List<FcUnit> fcUnits;
        if (buildingCode.equals("*")) {
            queryWrapper.select("*");
            fcUnits = fcUnitMapper.selectList(queryWrapper);
        } else {
            queryWrapper.eq("building_code", buildingCode);
            //queryWrapper.select("unit_code", "unit_name");
            fcUnits = fcUnitMapper.selectList(queryWrapper);
        }
        return fcUnits;
    }

    /**
     * 根据单元编码查询房间信息
     *
     * @param unitCode 单元编码
     * @return 返回房间信息
     */
    @Override
    public List<FcCell> selectCell(String unitCode) {
        QueryWrapper<FcCell> queryWrapper = new QueryWrapper<>();
        List<FcCell> fcCells;
        if (unitCode.equals("")) {
            queryWrapper.select("*");
            fcCells = fcCellMapper.selectList(queryWrapper);
        } else {
            queryWrapper.eq("unit_code", unitCode);
            fcCells = fcCellMapper.selectList(queryWrapper);
        }
        return fcCells;
    }

    /**
     * 根据公司信息查询住宅信息
     *
     * @param company 公司信息
     * @return 住宅信息
     */
    @Override
    public List<FcEstate> selectEstate(String company) {
        QueryWrapper<FcEstate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company", company);
        List<FcEstate> fcEstates = fcEstateMapper.selectList(queryWrapper);
        return fcEstates;
    }

    /**
     * 查询所有住宅信息
     *
     * @return 返回住宅信息
     */
    @Override
    public List<FcEstate> selectAllEstate() {
        List<FcEstate> fcEstates = fcEstateMapper.selectAllEstate();
        return fcEstates;
    }

    @Override
    public List<FcBuilding> selectBuildingByEstateCode(String estateCode) {
        /**
         * 思路：当前端发送请求携带参数时，做条件的查询，如果
         * 前端请求参数没有具体值时，做全量的数据查询
         */
        List<FcBuilding> fcBuildings;
        QueryWrapper<FcBuilding> queryWrapper = new QueryWrapper<>();
        if(estateCode.equals("")) {
            queryWrapper.select("*");
            fcBuildings = fcBuildingMapper.selectList(queryWrapper);
        } else {
            queryWrapper.eq("estate_code", estateCode);
            fcBuildings = fcBuildingMapper.selectList(queryWrapper);
        }
        return fcBuildings;
    }

    @Override
    public FcBuilding selectBuildingByEstateCodeAndBuildingCode(String estateCode, String buildingCode) {
        QueryWrapper<FcBuilding> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("estate_code", estateCode);
        queryWrapper.eq("building_code", buildingCode);
        FcBuilding fcBuilding = fcBuildingMapper.selectOne(queryWrapper);
        return fcBuilding;
    }

    /**
     * 建筑面积，使用面积
     * @param fcBuilding 面积
     * @return 修改，回显
     */
    /*@Override
    public Integer selectEstate(FcBuilding fcBuilding) {
        Integer result = fcBuildingMapper.updateById(fcBuilding);
        return result;
    }*/


}
